package com.lifeng.sso.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.lifeng.sso.domain.User;

public class LoginIntercept implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object obj) throws Exception {
		String url = "";
		url = request.getScheme() + "://" 
		          + request.getServerName() + ":" + request.getServerPort() 
		          + request.getRequestURI() 
		          + (request.getQueryString() != null ? "?" + request.getQueryString() : "");
		System.out.println("当前请求的url:"+url);
		User user = (User) request.getSession().getAttribute("user");
		if(user != null){
			return true;
		}
		System.out.println("======>您未登录");
		response.sendRedirect("http://mysso-passport.lifeng.com:8081/mysso-passport/user/loginUI?url="+url);
		return false;
	}

}
